import { Base } from '../../utils/base.js';

class Product extends Base {
  constructor() {
    super();
  }
//获取商品详情信息
  getDetailInfo(params, callback) {
    var param = {
      url: 'product/detail',
      data: params,
      type: 'post',
      sCallback: function (data) {
        callback && callback(data);
      }
    };
    
    this.request(param);
  }

  //获取sku信息
  getSkuData(id, callback){
    var that=this;
    var param = {
      url: 'product/skus',
      data: { 'product_id': id },
      type: 'post',
      sCallback: function (data) {
        that.setAttrValuesStock(data);
        callback && callback(data);
      }
    };
    this.request(param);
  }

  //初始化商品stock
  setAttrValuesStock(data) {
    var attributes = data.attributes && data.attributes;
    var skus = data.skus && data.skus;
    var skus_len = data.skus && data.skus && data.skus.length;
    var attr_len = data.attributes && data.attributes && data.attributes.length;
    var appearanceAttrValues = new Array();
    var skuStockArray = new Array();//有库存的sku
    for (var i = 0; i < skus_len; i++) {
      if (skus[i].stock > 0) {
        var skuId = skus[i].sku_id;
        var skuValueArray = skuId.slice(skuId.indexOf('_') + 1).split("_");
        skuStockArray.push(skuValueArray);
        for (var j = 0; j < skuValueArray.length; j++) {
          if (appearanceAttrValues.indexOf(skuValueArray[j]) < 0) {
            appearanceAttrValues.push(skuValueArray[j]);
          }
        }
      }
    }
    for (var i = 0; i < attr_len; i++) {
      //初始化stock和获取select的valueId
      for (var m = 0; m < attributes[i].child.length; m++) {
        for (var k = 0; k < appearanceAttrValues.length; k++) {
          if (appearanceAttrValues[k] == attributes[i].child[m].value_id) {
            attributes[i].child[m].stock = 1;//所有的value的stock初始化为1;  
          }
        }
      }
    }
    //如果只有一个规格值则选中
    for (var i = 0; i < attr_len;i++){
      var attr_child_len = data.attributes[i].child.length
      if(attr_child_len==1){
        data.attributes[i].child[0].is_select = 1;
      }
    }
  }

  /*切换商品属性值*/
  changAttr(event, productDetail) {
    var selectIndex = this.getDataSet(event, 'selectIndex');//行（选择的）
    var attrIndex = this.getDataSet(event, 'attrIndex');//列（选择的）
    var attributes = productDetail.attributes;//规格，child为规格值
    var skus = productDetail.skus;//SKu
    var count = attributes[selectIndex].child.length;//当前选中的规格的规格值总数
    if (attributes[selectIndex].child[attrIndex].is_select == 0) {//如果当前点中的规格值不是选中状态，设置为选中，并且同级规格值设置为不选中
      for (var i = 0; i < count; i++) {
        attributes[selectIndex].child[i].is_select = 0;
      }
      attributes[selectIndex].child[attrIndex].is_select = 1;//设置为选中
    } else {
      attributes[selectIndex].child[attrIndex].is_select = 0;//如果当前点中的规格值不是选中状态，设置为选中
    }
    var appearanceAttrValues = new Array();
    var skuStockArray = new Array();//有库存的sku，二维数组[arrIndex][attrvalue1,attrvalue2,....]
    for (var i = 0; i < skus.length; i++) {
        if(skus[i].stock>0){
          var skuId = skus[i].sku_id;
          var skuValueArray = skuId.slice(skuId.indexOf('_')+1).split("_");
          skuStockArray.push(skuValueArray);
          for (var j = 0; j < skuValueArray.length;j++){
            if (appearanceAttrValues.indexOf(skuValueArray[j])<0){
              appearanceAttrValues.push(skuValueArray[j]);
            }
          }
        }
    } 
    var selectedChild = new Array();//select的valueId的二维数组。即每一行选中的value值数组:[row][value_id]
    var emptyStock = new Array();
    var attrValues = new Array();//所有的attr的value二维数组[row][attrvalue1,attrvalue2....]
    for (var i = 0; i < attributes.length; i++) { 
      selectedChild[i] = new Array();  
      attrValues[i]= new Array();
      emptyStock[i] = new Array();
      //初始化stock和  
      //设置selectedChild
      for (var m = 0; m < attributes[i].child.length; m++) {
        attrValues[i].push(attributes[i].child[m].value_id);            
        if (attributes[i].child[m].is_select == 1) {
          selectedChild[i].push(attributes[i].child[m].value_id);
        }
      }
    }
    for (var i = 0; i < selectedChild.length; i++) { 
      if (selectedChild[i].length<=0){
        continue;
      }  
      var copyAttrValues = this.deepcopy(attrValues)//即将填充到emptyStock的值，等同于emptyStock
      var selectedValue = selectedChild[i][0];//第i行选中的值
      //遍历整个skustockArray
      for(var j=0;j<skuStockArray.length;j++){
        var skuStock = skuStockArray[j];//有库存的sku数组的value值数组[attrvalue1,attrvalue2]，里面的value分别对应不同的attr
        if (skuStock.indexOf(selectedValue.toString())>-1){//找出所有包含该selectedValue的skuStock；
          for(var k = 0;k<skuStock.length;k++){
            if(skuStock[k]==selectedValue){
              copyAttrValues[k] = new Array();//设置同一行的所有的value值为空
              continue;
            }else{
              var index = copyAttrValues[k].indexOf(parseInt(skuStock[k]));
              if (index > -1) {
                copyAttrValues[k].splice(index, 1);
              }
            }
          }
        }     
       
      }  
      emptyStock[i] = this.deepcopy(copyAttrValues);     
    }
    var emptyStockValues = new Array();
    for (var i = 0; i < emptyStock.length; i++) {
      for (var j = 0; j < emptyStock[i].length; j++) {
        for (var k = 0; k < emptyStock[i][j].length;k++){
          var emptyStockValue = emptyStock[i][j][k];
          if (emptyStockValues.indexOf(emptyStockValue)<0){
            emptyStockValues.push(emptyStockValue);
          }          
        }
      }
    }

    for (var i = 0; i < attributes.length; i++) {
      if (emptyStockValues.length==0) {
          for (var m = 0; m < attributes[i].child.length; m++) {           
            attributes[i].child[m].stock = 1;//stock设置为0;            
          }
      } else{
        for(var j=0;j<emptyStockValues.length;j++){
          for (var m = 0; m < attributes[i].child.length; m++) {
            if (emptyStockValues[j] == attributes[i].child[m].value_id) {
              attributes[i].child[m].stock = 0;//stock设置为0;   
            }
          }
        } 
      }     
      
    }

    
    return attributes;
  }
  deepcopy(obj) {
      var out = [], i = 0, len = obj.length;
      for (; i < len; i++) {
        if (obj[i] instanceof Array) {
          out[i] = this.deepcopy(obj[i]);
        }
        else out[i] = obj[i];
      }
      return out;
  }

  /*调整商品价格图片库存等信息*/
  changProductinf(productDetail) {
    if (this.isSelectAll(productDetail).skustr){
      var skustr = "";
      skustr = this.isSelectAll(productDetail).skustr;
      for (var i = 0; i < productDetail.skus.length; i++) {
        if (productDetail.skus[i].sku_id == skustr){
          return i ;
          break;
        }
      }
    }
  }

  isSelectAll(productDetail){
    var attributes = productDetail.attributes;
    var skus = [],unselected = [];
    for (var i = 0; i < attributes.length; i++) {
      for (var j = 0; j < attributes[i].child.length; j++) {
        if (attributes[i].child[j].is_select == 1) {
          var skuobj = {};
          skuobj.value_id = attributes[i].child[j].value_id;
          skuobj.value_str = attributes[i].child[j].value_str;
          skus.push(skuobj);
        }
      }
      if (!skus[i])
      {
        skus[i]="unselected";  //没有选的属性组，标记为未选
        unselected.push(attributes[i].attribute_name); 
      }
    }
    var unselectLabel = unselected.join(" ");
    if (!unselectLabel) {
      var skustr = productDetail.product.product_id;
      var selectLabel = '已选';
      for (var i = 0; i < skus.length; i++) {
        skustr += '_' + skus[i].value_id;     //构造 12_34_56_78形式，第一段数字是商品id
        selectLabel += ' ' + skus[i].value_str;
      }
      return {selectLabel, skustr}
    }
    else{
      return {unselectLabel};
    }
  }

  /*调整商品数目*/
  changeCounts(event, productCounts,maxCounts) {
    var mark = this.getDataSet(event, 'type');
      currentNum = 1;
    if (mark == '1') {
      var currentNum = productCounts;
      currentNum++;
    } else if (mark == '-1' && productCounts > 1) {
      var currentNum = productCounts;
      currentNum--;
    }

    return currentNum;
  }
  //获取商品分享二维码
  qrcode(params, callback) {
    var that = this;
    var param = {
      url: 'xcx-member/qrcode',
      data: params,
      type: 'post',
      sCallback: function (data) {
        callback && callback(data);
      }
    };
    this.request(param);
  }

};

export { Product }